import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isArray from "lodash/isArray";

var _dec, _dec2, _dec3, _class;

/**
 * Table - 角色管理树形 - 列表页面表格
 * @date: 2018-7-4

 */
import React, { PureComponent } from 'react';
import { Bind } from 'lodash-decorators';
import { Badge, Icon, Table } from 'antd';
import { Button as ButtonPermission } from 'components/Permission';
import intl from 'utils/intl';
import { getCodeMeaning, isTenantRoleLevel, tableScrollWidth } from 'utils/utils';
import { VERSION_IS_OP } from 'utils/config';
import { operatorRender } from 'utils/renderer';
var List = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), (_class = /*#__PURE__*/function (_PureComponent) {
  _inherits(List, _PureComponent);

  var _super = _createSuper(List);

  function List() {
    var _this;

    _classCallCheck(this, List);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.defaultTableRowKey = 'id';
    return _this;
  }

  _createClass(List, [{
    key: "optionsRender",
    value: function optionsRender(text, record) {
      var _this$props = this.props,
          _this$props$handleAct = _this$props.handleAction,
          handleAction = _this$props$handleAct === void 0 ? function (e) {
        return e;
      } : _this$props$handleAct,
          tenantRoleLevel = _this$props.tenantRoleLevel,
          path = _this$props.path;
      var operators = []; // assignedFlag   4 分配标志        -> 创建角色
      // adminFlag      2 管理标志
      // haveAdminFlag  1 有父级角色标志

      var branch = (record.assignedFlag === 1 ? 4 : 0) + (record.adminFlag === 1 ? 2 : 0) + (record.haveAdminFlag === 1 ? 1 : 0); // 创建角色

      var createRoleBtn = {
        key: 'create',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.roleCreate"),
            type: 'button',
            meaning: '角色管理树形-创建角色'
          }],
          onClick: function onClick() {
            return handleAction('create', record, true);
          }
        }, intl.get('ciam.roleManagement.view.button.roleCreate').d('创建角色')),
        len: 4,
        title: intl.get('ciam.roleManagement.view.button.roleCreate').d('创建角色')
      }; // 分配用户

      var assignMemberBtn = {
        key: 'assign-members',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.members"),
            type: 'button',
            meaning: '角色管理树形-分配用户'
          }],
          onClick: function onClick() {
            return handleAction('editMembers', record, true);
          }
        }, intl.get("ciam.roleManagement.view.title.members").d('分配用户')),
        len: 4,
        title: intl.get("ciam.roleManagement.view.title.members").d('分配用户')
      }; // 分配用户

      var assignClientBtn = {
        key: 'assign-client',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.clients"),
            type: 'button',
            meaning: '角色管理树形-分配客户端'
          }],
          onClick: function onClick() {
            return handleAction('editClients', record, true);
          }
        }, intl.get("ciam.roleManagement.view.title.clients").d('分配客户端')),
        len: 5,
        title: intl.get("ciam.roleManagement.view.title.clients").d('分配客户端')
      }; // 分配权限

      var assignPermission = {
        key: 'assign-permissions',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.assignPermissions"),
            type: 'button',
            meaning: '角色管理树形-分配权限'
          }],
          onClick: function onClick() {
            return handleAction('assignPermissions', record, true);
          }
        }, intl.get("ciam.roleManagement.view.button.assignPermissions").d('分配权限')),
        len: 4,
        title: intl.get("ciam.roleManagement.view.button.assignPermissions").d('分配权限')
      }; // 复制

      var copyBtn = {
        key: 'copy',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.copy"),
            type: 'button',
            meaning: '角色管理树形-复制'
          }],
          onClick: function onClick() {
            return handleAction('copy', record, true);
          }
        }, intl.get("cpaas.common.button.copy").d('复制')),
        len: 2,
        title: intl.get("cpaas.common.button.copy").d('复制')
      }; // 继承

      var inheritBtn = {
        key: 'inherit',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.inherit"),
            type: 'button',
            meaning: '角色管理树形-继承'
          }],
          onClick: function onClick() {
            return handleAction('inherit', record, true);
          }
        }, intl.get("ciam.roleManagement.view.title.button.inherit").d('继承')),
        len: 2,
        title: intl.get("ciam.roleManagement.view.title.button.inherit").d('继承')
      }; // 编辑

      var editBtn = {
        key: 'edit',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.edit"),
            type: 'button',
            meaning: '角色管理树形-编辑'
          }],
          onClick: function onClick() {
            return handleAction('edit', record, true);
          }
        }, intl.get("cpaas.common.button.edit").d('编辑')),
        len: 2,
        title: intl.get("cpaas.common.button.edit").d('编辑')
      }; // 启用/禁用

      var enableBtn = {
        key: 'enabled',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.status"),
            type: 'button',
            meaning: '角色管理树形-状态'
          }],
          onClick: function onClick() {
            return handleAction('enabled', record, true);
          }
        }, record.enabled ? intl.get("cpaas.common.status.disable").d('禁用') : intl.get("cpaas.common.status.enable").d('启用')),
        len: 2,
        title: record.enabled ? intl.get("cpaas.common.status.disable").d('禁用') : intl.get("cpaas.common.status.enable").d('启用')
      }; // 工作台配置-分配卡片

      var cardBtn = {
        key: 'assign-role',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.assignCards"),
            type: 'button',
            meaning: '角色管理树形-工作台配置'
          }],
          onClick: function onClick() {
            return handleAction('assignCards', record, true);
          }
        }, intl.get("ciam.roleManagement.view.title.assignCards").d('工作台配置')),
        len: 5,
        title: intl.get("ciam.roleManagement.view.title.assignCards").d('工作台配置')
      }; // 维护数据权限

      var dataPermissionBtn = {
        key: 'editPermission',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.editPermission"),
            type: 'button',
            meaning: '角色管理树形-维护数据权限'
          }],
          onClick: function onClick() {
            return handleAction('editPermission', record, true);
          }
        }, intl.get("ciam.roleManagement.view.button.button.editPermission").d('维护数据权限')),
        len: 6,
        title: intl.get("ciam.roleManagement.view.button.button.editPermission").d('维护数据权限')
      }; // 字段权限维护-Api字段权限维护-角色

      var fieldPermissionBtn = {
        key: 'field-permission-maintain',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.fieldPermission"),
            type: 'button',
            meaning: '角色管理树形-字段权限维护'
          }],
          onClick: function onClick() {
            return handleAction('editFieldPermission', record, true);
          }
        }, intl.get('ciam.roleManagement.view.button.fieldPermission').d('维护字段权限')),
        len: 6,
        title: intl.get('ciam.roleManagement.view.button.fieldPermission').d('维护字段权限')
      }; // 分配安全组

      var secGrpBtn = {
        key: 'security-group',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.securityGroup"),
            type: 'button',
            meaning: '角色管理-分配安全组'
          }],
          onClick: function onClick() {
            return handleAction('editSecurityGroup', record, true);
          }
        }, intl.get('ciam.roleManagement.view.button.secGrp').d('分配安全组')),
        len: 5,
        title: intl.get('ciam.roleManagement.view.button.secGrp').d('分配安全组')
      };
      /* eslint-disable no-fallthrough */

      switch (branch) {
        case 6:
          // 分配&管理
          operators.push(createRoleBtn, copyBtn, inheritBtn, assignMemberBtn, assignClientBtn);
          break;

        case 7:
          // 分配&管理&父级管理角色
          operators.push(createRoleBtn);

        case 5: // 分配&父级管理角色

        case 1:
          // 父级管理角色
          operators.push(copyBtn, inheritBtn, editBtn, enableBtn, assignMemberBtn, assignClientBtn);

          if (record.enabled) {
            // 角色启用
            operators.push(assignPermission, secGrpBtn);
          }

          break;

        case 4: // 分配

        default:
          return null;
      }
      /* eslint-enable no-fallthrough */


      if (record.enabled) {
        operators.push(cardBtn, dataPermissionBtn);

        if (tenantRoleLevel) {
          // 租户
          operators.push(fieldPermissionBtn);
        }
      } // 只有 `创建角色`,`复制`,`继承`,`编辑` 在外面 其他的操作都在 操作下拉菜单中


      var operatorLimit = 4;

      if (branch === 6) {
        operatorLimit = 3;
      }

      return operatorRender(operators, record, {
        limit: operatorLimit
      });
    }
    /**
     * 展开/收起行
     * @param {boolean} expanded - 是否展开
     * @param {*} record - 表格当前行
     */

  }, {
    key: "handleExpand",
    value: function handleExpand(expanded, record) {
      this.props.onExpand(expanded, record);
    }
    /**
     * 渲染角色名称列
     * @param {object} record - 表格当前行数据
     */

  }, {
    key: "renderRoleNameColumn",
    value: function renderRoleNameColumn(_, record) {
      var _this$props2 = this.props,
          path = _this$props2.path,
          currentRoleId = _this$props2.currentRoleId,
          childrenLoading = _this$props2.childrenLoading,
          _this$props2$onFetchC = _this$props2.onFetchChildren,
          onFetchChildren = _this$props2$onFetchC === void 0 ? function (e) {
        return e;
      } : _this$props2$onFetchC;
      var name = record.name,
          childrenNum = record.childrenNum,
          children = record.children;
      var pageSize = 10;
      var item = name;

      if (_isArray(children)) {
        var length = children.length;
        var more = currentRoleId === record.id && childrenLoading ? /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.loading"),
            type: 'button',
            meaning: '角色管理树形-loading'
          }]
        }, /*#__PURE__*/React.createElement(Icon, {
          type: "loading"
        })) : length > 0 && childrenNum > length && /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.more"),
            type: 'button',
            meaning: '角色管理树形-更多'
          }],
          onClick: function onClick() {
            return onFetchChildren({
              parentRoleId: record.id,
              levelPath: record.levelPath,
              page: {
                current: Math.floor(length / pageSize) + 1,
                pageSize: pageSize
              }
            });
          }
        }, intl.get('ciam.roleManagement.view.button.more').d('更多'));
        var count = childrenNum ? "\uFF08".concat(childrenNum, "\uFF09") : null;
        item = /*#__PURE__*/React.createElement("span", null, name, count, more);
      }

      return item;
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          loading = _this$props3.loading,
          _this$props3$dataSour = _this$props3.dataSource,
          dataSource = _this$props3$dataSour === void 0 ? [] : _this$props3$dataSour,
          code = _this$props3.code,
          _this$props3$onListCh = _this$props3.onListChange,
          onListChange = _this$props3$onListCh === void 0 ? function (e) {
        return e;
      } : _this$props3$onListCh,
          tenantsMulti = _this$props3.tenantsMulti,
          expandedRowKeys = _this$props3.expandedRowKeys,
          pagination = _this$props3.pagination,
          isHaveParams = _this$props3.isHaveParams;
      var isTenant = isTenantRoleLevel();
      var columns = [{
        dataIndex: 'name',
        title: intl.get("ciam.roleManagement.model.roleManagement.name").d('角色名称'),
        render: this.renderRoleNameColumn,
        width: 200,
        fixed: 'left'
      }, {
        dataIndex: 'code',
        title: intl.get("ciam.roleManagement.model.roleManagement.code").d('角色编码'),
        width: isTenant ? 300 : 150,
        fixed: 'left'
      }, !VERSION_IS_OP && !isTenant && {
        dataIndex: 'levelMeaning',
        title: intl.get("ciam.roleManagement.model.roleManagement.level").d('角色层级'),
        width: 120
      }, {
        dataIndex: 'parentRoleName',
        title: intl.get('ciam.roleManagement.model.roleManagement.topRole').d('上级角色'),
        width: 150
      }, !isTenant && {
        dataIndex: 'roleSource',
        title: intl.get("ciam.roleManagement.model.roleManagement.roleSource").d('角色来源'),
        width: 120,
        render: function render(text) {
          return getCodeMeaning(text, code);
        }
      }, !VERSION_IS_OP && (!isTenant || tenantsMulti) && {
        dataIndex: 'tenantName',
        title: intl.get("ciam.roleManagement.model.roleManagement.tenant").d('所属租户'),
        width: 150
      }, {
        dataIndex: 'inheritedRoleName',
        title: intl.get("ciam.roleManagement.model.roleManagement.inheritedRole").d('继承自'),
        width: 150
      }, {
        dataIndex: 'enabled',
        title: intl.get("cpaas.common.status").d('状态'),
        width: 90,
        render: function render(text, record) {
          return /*#__PURE__*/React.createElement(Badge, {
            status: record.enabled ? 'success' : 'error',
            text: record.enabled ? intl.get("cpaas.common.status.enable").d('启用') : intl.get("ciam.roleManagement.view.title.disable").d('禁用')
          });
        }
      }, {
        dataIndex: 'createdUserName',
        title: intl.get('ciam.roleManagement.model.roleManagement.createdUserName').d('创建人'),
        width: 200
      }, {
        // TODO: 由于 levelPath 挪作他用, 所以这里使用 _levelPath 存储原来的 levelPath 值
        dataIndex: isHaveParams ? 'levelPath' : '_levelPath',
        title: intl.get('ciam.roleManagement.model.roleManagement.levelPath').d('角色路径')
      }, {
        key: 'operator',
        title: intl.get('cpaas.common.button.action').d('操作'),
        width: 290,
        fixed: 'right',
        render: this.optionsRender
      }].filter(Boolean);
      var tableProps = {
        loading: loading,
        dataSource: dataSource,
        pagination: pagination,
        columns: columns,
        bordered: true,
        rowKey: 'id',
        expandedRowKeys: expandedRowKeys,
        onExpand: this.handleExpand,
        onChange: onListChange,
        scroll: {
          x: tableScrollWidth(columns)
        }
      };
      return /*#__PURE__*/React.createElement(Table, tableProps);
    }
  }]);

  return List;
}(PureComponent), (_applyDecoratedDescriptor(_class.prototype, "optionsRender", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "optionsRender"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleExpand", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleExpand"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "renderRoleNameColumn", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "renderRoleNameColumn"), _class.prototype)), _class));
export default List;